package gestionServers.service;

import image.imagemanager.*;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.StringTokenizer;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.xml.ws.WebServiceException;


@WebService(name = "ServerManager", targetNamespace = "gestionServers.service")
@SOAPBinding(style=SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.LITERAL)
public class ServerManager {
	Hashtable<String,ImageManager> services;

	public ServerManager() {
		services = new Hashtable<String, ImageManager>();
	}
	
	public ServerManager(Hashtable<String,String> services) {
		URL url;
		this.services = new Hashtable<String,ImageManager>();
		
		for(Enumeration<String> e = services.keys(); e.hasMoreElements();) {
			try {
				String nomServeur = e.nextElement();
				url = new URL(services.get(nomServeur) + "?wsdl");
				ImageManager service = new ImageManagerService(url).getImageManagerPort();
				this.services.put(nomServeur, service);
			} 
			catch (MalformedURLException e1) {
				e1.printStackTrace();
			}
		}
	}
	
	@WebMethod(operationName = "connect")
	@WebResult(name = "clientID", targetNamespace = "gestionServers.service")
	public String connect() throws NoServerAvailableException {
		//On cherche le serveur possédant la charge minimum
		int chargeMin = Integer.MAX_VALUE;
		ImageManager bestService = null;
		
		for(Enumeration<ImageManager> e = services.elements(); e.hasMoreElements();) {
			ImageManager service = e.nextElement();
			try {
				if(service.getCharge() < chargeMin && service.clientAvailable()) {
					chargeMin = service.getCharge();
					bestService = service;
				}
			}
			//Si un service ne répond pas, on l'ignore simplement
			catch (WebServiceException ex) {}
		}
		
		try {
			if(bestService != null)
				return bestService.getConnection();
			else {
				//Tous les serveurs sont plein : on en informe le client
				throw new NoServerAvailableException("Désolé, aucun serveur disponible, veuillez réessayer plus tard.");
			}
		}
		catch(WebServiceException ex) {
			return null;
		}
	}
	
	@WebMethod(operationName = "disconnect")
	@WebResult(name = "reussi", targetNamespace = "gestionServers.service")
	public boolean disconnect(
			@WebParam(name = "clientID", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String clientID) throws BadClientIDException {
			
		ImageManager service = getServerID(clientID);
		
		if(service != null) {
			try {
				if(service.isCorrectID(clientID))
						return service.disconnect(clientID);
				else
					throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
			}
			catch(WebServiceException ex) {
				return false;
			}
		}
		else
			throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
	}
	
	/**
	 * Permet de récupérer le proxy du serveur grâce à son identifiant contenu dans celui du client.
	 * 
	 * @param clientID L'identifiant du client.
	 * @return Le proxy du service, null si ce service n'existe pas.
	 */
	private ImageManager getServerID(String clientID) {
		StringTokenizer tokenizer = new StringTokenizer(clientID, "-");
		
		return services.get(tokenizer.nextToken());
	}
	
	@WebMethod(operationName = "CreateImage")
	@WebResult(name = "reussi")
	public boolean createImage(
			@WebParam(name = "clientID", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String clientID,
			@WebParam(name = "imageName", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String nom,
			@WebParam(name = "width", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int width,
			@WebParam(name = "height", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int height) throws ServerNotAvailableException, FileAlreadyExistsException, BadClientIDException {
		
		ImageManager service = getServerID(clientID);
		
		if(service != null) {
			try {
				if(service.isCorrectID(clientID))
						return service.createImage(nom, width, height);
				else
					throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
			}
			catch(WebServiceException ex) {
				throw new ServerNotAvailableException("Désolé, le serveur attribué n'est plus disponible, veuillez vous reconnecter.");
			}
			catch(FileAlreadyExistsException_Exception ef) {
				throw new FileAlreadyExistsException(ef.getMessage());
			}
		}
		else
			throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
	}
	
	@WebMethod(operationName = "DeleteImage")
	@WebResult(name = "reussi")
	public boolean deleteImage(
			@WebParam(name = "clientID", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String clientID,
			@WebParam(name = "imageName", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String nom) throws ServerNotAvailableException, NoFileFoundException, BadClientIDException {
		
		ImageManager service = getServerID(clientID);
		
		if(service != null) {
			try {
				if(service.isCorrectID(clientID))
					return service.deleteImage(nom);
				else
					throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
			}
			catch(WebServiceException ex) {
				throw new ServerNotAvailableException("Désolé, le serveur attribué n'est plus disponible, veuillez vous reconnecter.");
			}
			catch(NoFileFoundException_Exception ef) {
				throw new NoFileFoundException(ef.getMessage());
			}
		}
		else
			throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
	}
	
	@WebMethod(operationName = "AddPoint")
	@WebResult(name = "reussi")
	public boolean addPoint(
			@WebParam(name = "clientID", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String clientID,
			@WebParam(name = "imageName", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String nom,
			@WebParam(name = "x", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int x,
			@WebParam(name = "y", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int y,
			@WebParam(name = "r", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int r,
			@WebParam(name = "g", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int g,
			@WebParam(name = "b", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int b) throws ServerNotAvailableException, NoFileFoundException, BadClientIDException {
		
		ImageManager service = getServerID(clientID);
		
		if(service != null) {
			try {
				if(service.isCorrectID(clientID))
					return service.addPoint(nom, x, y, r, g, b);
				else
					throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
			}
			catch(WebServiceException ex) {
				throw new ServerNotAvailableException("Désolé, le serveur attribué n'est plus disponible, veuillez vous reconnecter.");
			}
			catch(NoFileFoundException_Exception ef) {
				throw new NoFileFoundException(ef.getMessage());
			}
		}
		else
			throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
	}
	
	@WebMethod(operationName = "AddRectangle")
	@WebResult(name = "reussi")
	public boolean addRectangle(
			@WebParam(name = "clientID", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String clientID,
			@WebParam(name = "imageName", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String nom,
			@WebParam(name = "x", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int x,
			@WebParam(name = "y", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int y,
			@WebParam(name = "r", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int r,
			@WebParam(name = "g", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int g,
			@WebParam(name = "b", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int b,
			@WebParam(name = "r2", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int r2,
			@WebParam(name = "g2", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int g2,
			@WebParam(name = "b2", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int b2,
			@WebParam(name = "fill", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			boolean fill,
			@WebParam(name = "longueur", mode = WebParam.Mode.IN, targetNamespace = "image")
			int longueur,
			@WebParam(name = "largeur", mode = WebParam.Mode.IN, targetNamespace = "image")
			int largeur) throws ServerNotAvailableException, NoFileFoundException, BadClientIDException {
		
		ImageManager service = getServerID(clientID);
		
		if(service != null) {
			try {
				if(service.isCorrectID(clientID))
					return service.addRectangle(nom, x, y, r, g, b, r2, g2, b2, fill, longueur, largeur);
				else
					throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
			}
			catch(WebServiceException ex) {
				throw new ServerNotAvailableException("Désolé, le serveur attribué n'est plus disponible, veuillez vous reconnecter.");
			}
			catch(NoFileFoundException_Exception ef) {
				throw new NoFileFoundException(ef.getMessage());
			}
		}
		else
			throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
	}
	
	@WebMethod(operationName = "AddArc")
	@WebResult(name = "reussi")
	public boolean addArc(
			@WebParam(name = "clientID", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String clientID,
			@WebParam(name = "imageName", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String nom,
			@WebParam(name = "x", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int x,
			@WebParam(name = "y", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int y,
			@WebParam(name = "r", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int r,
			@WebParam(name = "g", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int g,
			@WebParam(name = "b", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int b,
			@WebParam(name = "r2", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int r2,
			@WebParam(name = "g2", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int g2,
			@WebParam(name = "b2", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int b2,
			@WebParam(name = "fill", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			boolean fill,
			@WebParam(name = "longueur", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int longueur,
			@WebParam(name = "largeur", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int largeur,
			@WebParam(name = "AngleDebut", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int angleDebut,
			@WebParam(name = "AngleFin", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int angleFin) throws ServerNotAvailableException, NoFileFoundException, BadClientIDException {
		
		ImageManager service = getServerID(clientID);
		
		if(service != null) {
			try {
				if(service.isCorrectID(clientID))
					return service.addArc(nom, x, y, r, g, b, r2, g2, b2, fill, longueur, largeur, angleDebut, angleFin);
				else
					throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
			}
			catch(WebServiceException ex) {
				throw new ServerNotAvailableException("Désolé, le serveur attribué n'est plus disponible, veuillez vous reconnecter.");
			}
			catch(NoFileFoundException_Exception ef) {
				throw new NoFileFoundException(ef.getMessage());
			}
		}
		else
			throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
	}
	
	@WebMethod(operationName = "AddEllipse")
	@WebResult(name = "reussi")
	public boolean addEllipse(
			@WebParam(name = "clientID", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String clientID,
			@WebParam(name = "imageName", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String nom,
			@WebParam(name = "x", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int x,
			@WebParam(name = "y", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int y,
			@WebParam(name = "r", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int r,
			@WebParam(name = "g", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int g,
			@WebParam(name = "b", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int b,
			@WebParam(name = "r2", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int r2,
			@WebParam(name = "g2", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int g2,
			@WebParam(name = "b2", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int b2,
			@WebParam(name = "fill", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			boolean fill,
			@WebParam(name = "longueur", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int longueur,
			@WebParam(name = "largeur", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int largeur) throws ServerNotAvailableException, NoFileFoundException, BadClientIDException {
		
		ImageManager service = getServerID(clientID);
		
		if(service != null) {
			try {
				if(service.isCorrectID(clientID))
					return service.addEllipse(nom, x, y, r, g, b, r2, g2, b2, fill, longueur, largeur);
				else
					throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
			}
			catch(WebServiceException ex) {
				throw new ServerNotAvailableException("Désolé, le serveur attribué n'est plus disponible, veuillez vous reconnecter.");
			}
			catch(NoFileFoundException_Exception ef) {
				throw new NoFileFoundException(ef.getMessage());
			}
		}
		else
			throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
	}
	
	@WebMethod(operationName = "AddLigne")
	@WebResult(name = "reussi")
	public boolean addLigne(
			@WebParam(name = "clientID", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String clientID,
			@WebParam(name = "imageName", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String nom,
			@WebParam(name = "x", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int x,
			@WebParam(name = "y", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int y,
			@WebParam(name = "r", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int r,
			@WebParam(name = "g", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int g,
			@WebParam(name = "b", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int b,
			@WebParam(name = "x2", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int x2,
			@WebParam(name = "y2", mode = WebParam.Mode.IN, targetNamespace = "imageManager.image")
			int y2)  throws ServerNotAvailableException, NoFileFoundException, BadClientIDException {
		
		ImageManager service = getServerID(clientID);
		
		if(service != null) {
			try {
				if(service.isCorrectID(clientID))
					return service.addLigne(nom, x, y, r, g, b, x2, y2);
				else
					throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
			}
			catch(WebServiceException ex) {
				throw new ServerNotAvailableException("Désolé, le serveur attribué n'est plus disponible, veuillez vous reconnecter.");
			}
			catch(NoFileFoundException_Exception ef) {
				throw new NoFileFoundException(ef.getMessage());
			}
		}
		else
			throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
	}
	
	@WebMethod(operationName = "AddPolygone")
	@WebResult(name = "reussi")
	public boolean addPolygone(
			@WebParam(name = "clientID", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String clientID,
			@WebParam(name = "imageName", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String nom,
			@WebParam(name = "x", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int x,
			@WebParam(name = "y", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int y,
			@WebParam(name = "r", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int r,
			@WebParam(name = "g", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int g,
			@WebParam(name = "b", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int b,
			@WebParam(name = "r2", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int r2,
			@WebParam(name = "g2", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int g2,
			@WebParam(name = "b2", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int b2,
			@WebParam(name = "fill", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			boolean fill,
			@WebParam(name = "xCoords", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			List<Integer> xCoords,
			@WebParam(name = "yCoords", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			List<Integer> yCoords)  throws ServerNotAvailableException, NoFileFoundException, BadClientIDException {
		
		ImageManager service = getServerID(clientID);
		
		if(service != null) {
			try {
				if(service.isCorrectID(clientID))
					return service.addPolygone(nom, x, y, r, g, b, r2, g2, b2, fill, xCoords, yCoords);
				else
					throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
			}
			catch(WebServiceException ex) {
				throw new ServerNotAvailableException("Désolé, le serveur attribué n'est plus disponible, veuillez vous reconnecter.");
			}
			catch(NoFileFoundException_Exception ef) {
				throw new NoFileFoundException(ef.getMessage());
			}
		}
		else
			throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
	}
	
	@WebMethod(operationName = "addImage")
	@WebResult(name = "reussi")
	public boolean addImage(
			@WebParam(name = "clientID", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String clientID,
			@WebParam(name = "imageName", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String nom,
			@WebParam(name = "name", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String nomImage,
			@WebParam(name = "x", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int x,
			@WebParam(name = "y", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int y,
			@WebParam(name = "r", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int r,
			@WebParam(name = "g", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int g,
			@WebParam(name = "b", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			int b)  throws ServerNotAvailableException, NoFileFoundException, BadClientIDException {

		ImageManager service = getServerID(clientID);
		
		if(service != null) {
			try {
				if(service.isCorrectID(clientID))
					return service.addImage(nom, nomImage, x, y, r, g, b);
				else
					throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
			}
			catch(WebServiceException ex) {
				throw new ServerNotAvailableException("Désolé, le serveur attribué n'est plus disponible, veuillez vous reconnecter.");
			}
			catch(NoFileFoundException_Exception ef) {
				throw new NoFileFoundException(ef.getMessage());
			}
		}
		else
			throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
	}
		
	@WebMethod(operationName = "ListImages")
	@WebResult(name = "list")
	public List<String> listImages(
		@WebParam(name = "clientID", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
		String clientID) throws ServerNotAvailableException, BadClientIDException {
		
		ImageManager service = getServerID(clientID);
		
		if(service != null) {
			try {
				if(service.isCorrectID(clientID))
					return service.listImages();
				else
					throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
			}
			catch(WebServiceException ex) {
				throw new ServerNotAvailableException("Désolé, le serveur attribué n'est plus disponible, veuillez vous reconnecter.");
			}
		}
		else
			throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
	}
	
	@WebMethod(operationName = "getImage")
	@WebResult(name = "image", targetNamespace = "gestionServers.service")
	public byte[] getImage(
			@WebParam(name = "clientID", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String clientID,
			@WebParam(name = "imageName", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String nom,
			@WebParam(name = "format", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String format) throws ServerNotAvailableException, NoFileFoundException, BadClientIDException {
		
		ImageManager service = getServerID(clientID);
		
		if(service != null) {
			try {
				if(service.isCorrectID(clientID))
					return service.getImage(nom, format);
				else
					throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
			}
			catch(WebServiceException ex) {
				throw new ServerNotAvailableException("Désolé, le serveur attribué n'est plus disponible, veuillez vous reconnecter.");
			}
			catch(NoFileFoundException_Exception ef) {
				throw new NoFileFoundException(ef.getMessage());
			}
		}
		else
			throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
	}
	
	@WebMethod(operationName = "getXML")
	@WebResult(name = "XML")
	public String getXML(
			@WebParam(name = "clientID", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String clientID,
			@WebParam(name = "imageName", mode = WebParam.Mode.IN, targetNamespace = "gestionServers.service")
			String nom) throws ServerNotAvailableException, NoFileFoundException, BadClientIDException {
		
		ImageManager service = getServerID(clientID);
		
		if(service != null) {
			try {
				if(service.isCorrectID(clientID))
					return service.getXML(nom);
				else
					throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
			}
			catch(WebServiceException ex) {
				throw new ServerNotAvailableException("Désolé, le serveur attribué n'est plus disponible, veuillez vous reconnecter.");
			}
			catch(NoFileFoundException_Exception ef) {
				throw new NoFileFoundException(ef.getMessage());
			}
		}
		else
			throw new BadClientIDException("Désolé mais votre identifiant de client ne correspond à aucun serveur ! Tentez de vous connecter.");
	}
}
